package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Combo;
import com.itheima.service.ComboService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author 林晓伟
 */
@RestController
@RequestMapping("/setmeal")
public class ComboController {
    @Resource
    private ComboService comboService;

    /**
     * H17-新增套餐
     */
    @PostMapping
    public R add(@RequestBody Combo combo){
        Integer add = comboService.add(combo);
        if (add != 0){
            return R.success("新增套餐成功");
        }
        return R.error("新增套餐失败");
    }

    /**
     * H18-套餐分页查询
     * @param name 名字
     * @param page 页数
     * @param pageSize 每页条数
     */
    @GetMapping("/page")
    public R s(@RequestParam(value="name",required=false)String name,
               @RequestParam Integer page,
               @RequestParam Integer pageSize){
        Page<Combo> select = comboService.select(page, pageSize, name);
        return R.success(select);
    }

    /**
     * H19-删除套餐
     */
    @DeleteMapping
    public R delete(@RequestParam String ids){
        Integer delete = comboService.delete(ids);
        if (delete != 0){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    /**
     * H21-修改套餐
     */
    @PutMapping
    public R update(@RequestBody Combo combo){
        Integer update = comboService.update(combo);
        if (update != 0){
            return R.success("修改成功!");
        }
        return R.error("修改失败！");
    }

    /**
     * H20-根据id获取套餐
     */
    @GetMapping("/{id}")
    public R all(@PathVariable Integer id){
        Combo all = comboService.all(id);
        return R.success(all);
    }

    /**
     * H22-套餐上架/下架
     * @param ids 上架/下架 状态值
     */
    @PostMapping("/status/{status}")
    public R up(@PathVariable Integer status,@RequestParam String ids){
        Integer up = comboService.up(status,ids);
        if (up!= 0){
            return R.success("");
        }
        return R.error("");
    }


}
